<template>
  <div class="pageWrapper">
    <div class="colseBtn">
      <span class="iconfont iconicon-test"></span>
    </div>
    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <!-- 只有在父子件嵌套子组件的时候才知道这个子组件的作用 -->
    <!-- 父传子 -->
    <!-- 1.使用属性赋值给子组件 -->
    <AuthInput
      type="text"
      placeholder="用户名/手机号"
      :rule="/^\d{5,11}$/"
      err_message="请输入合法手机号"
      @valChange="setUsername"
    ></AuthInput>
    <AuthInput
      type="password"
      placeholder="请输入密码"
      :rule="/^\d{3,6}$/"
      err_message="请输入合法密码"
      @valChange="setPassword"
    ></AuthInput>

    <AuthBtn btnText="登录" @handleClick="Login"></AuthBtn>
  </div>
</template>

<script>
//嵌套子组件
// 1.引入
import AuthInput from "@/components/AuthInput";
import AuthBtn from "@/components/AuthBtn";

export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  components: {
    AuthInput,
    AuthBtn,
  },
  methods: {
    Login() {
      // console.log("父组件接收到子组件通知");
      // console.log(this.username);
      // console.log(this.password);
      if (!this.username || !this.password) {
        this.$toast.fail("请填写完整信息");
        return;
      }
      // 发起请求
      this.$axios({
        method: "post",
        url: "http://127.0.0.1:3000/login",
        data: {
          username: this.username,
          password: this.password,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.message == "登录成功") {
          this.$toast.success("登录成功");
          // 本地保存 token 和 用户 id
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userId", res.data.data.user.id);
          this.$router.push("/personalIndex");
        } else {
          this.$toast.fail(res.data.message);
        }
      });
    },
    setUsername(newUsername) {
      this.username = newUsername;
    },
    setPassword(newPassword) {
      this.password = newPassword;
    },
  },
};
</script>


<style lang="less" scoped>
.pageWrapper {
  background-image: linear-gradient(
    150deg,
    rgba(0, 106, 255, 0.925),
    rgba(255, 0, 255, 0.647)
  );
  padding: 24/360 * 100vw;
}
.iconicon-test {
  font-size: 26/360 * 100vw;
}
.logo {
  text-align: center;
}
.iconnew {
  font-size: 126/360 * 100vw;
  color: #cc3300;
}
</style>